//
//  WKDrawerViewController.m
//  WeKeTang
//
//  Created by ichina on 2017/8/22.
//  Copyright © 2017年 Dezhi Tiancheng Education Technology (Beijing) Co. Ltd. All rights reserved.
//

#import "WKDrawerViewController.h"

@interface WKDrawerViewController ()

@property (nonatomic, strong) UIViewController *showVC;
@property (nonatomic, strong) UIButton *coverBtn;
//左边菜单控制器最大显示范围
@property (nonatomic, assign) CGFloat leftWidth;
@property (nonatomic, strong) UIViewController *mainVc;
@property (nonatomic, strong) UIViewController *leftMenuVc;

@end

@implementation WKDrawerViewController

+(instancetype)shareDrawerVc{
    return (WKDrawerViewController*)[UIApplication sharedApplication].keyWindow.rootViewController;
}
- (UIStatusBarStyle)preferredStatusBarStyle {
    // 返回你所需要的状态栏样式
    return UIStatusBarStyleLightContent;
}
+(instancetype)drawerVcWithMainVc:(UIViewController*)mainVc leftMenuVc:(UIViewController*)leftMenuVc leftWidth:(CGFloat)leftWidth{
    
    WKDrawerViewController *drawerVc = [[WKDrawerViewController  alloc]init];
    drawerVc.mainVc = mainVc;
    drawerVc.leftWidth = leftWidth;
    drawerVc.leftMenuVc = leftMenuVc;
    //将左边菜单控制器的View添加到抽屉控制器的View上
    [drawerVc.view addSubview:leftMenuVc.view];
    //讲MainVc控制器的View添加到抽屉控制器的View上
    [drawerVc.view addSubview:mainVc.view];
    //如果两个控制器的view互为父子关系 则两个控制器也必须为父子关系
    [drawerVc addChildViewController:leftMenuVc];
    
    [drawerVc addChildViewController:mainVc];
    
    return drawerVc;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    //设置默认左边控制器的view向左偏移self.leftWidth
    self.leftMenuVc.view.transform = CGAffineTransformMakeTranslation(-self.leftWidth, 0);
    //添加阴影
    self.mainVc.view.layer.shadowColor = [UIColor blackColor].CGColor;
    self.mainVc.view.layer.shadowOffset = CGSizeMake(-3, -3);
    self.mainVc.view.layer.shadowOpacity = 0.2;
    self.mainVc.view.layer.shadowRadius = 5;
    for (UIViewController *VC in self.mainVc.childViewControllers) {
        [self addScreenEdgePanGestureRecognizeToView:VC.view];
    }
}
#pragma mark --- 获取主VC当前的导航控制器
- (WKNavgationController *)currentNavController{
    
    WKNavgationController *currentNavVC = [[WKNavgationController alloc] init];
    if ([self.mainVc isKindOfClass:[WKNavgationController class]]) {
        
       return (WKNavgationController *)self.mainVc;
    }
    return nil;
}
//添加手势
-(void)addScreenEdgePanGestureRecognizeToView:(UIView*)view{
    //边缘拖拽手势
    UIScreenEdgePanGestureRecognizer *pan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenEdgePanGestureRecognize:)];
    pan.edges = UIRectEdgeLeft;
    [view addGestureRecognizer:pan];
}
-(void)screenEdgePanGestureRecognize:(UIScreenEdgePanGestureRecognizer*)panGestureRecognize{
//    [self.leftMenuVc viewDidLoad];
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    //获得X方向拖拽说是的距离
    CGFloat offsetX = [panGestureRecognize translationInView:panGestureRecognize.view].x;
    self.mainVc.view.transform = CGAffineTransformMakeTranslation(offsetX, 0);
    //手势结束 或 取消
    if (panGestureRecognize.state == UIGestureRecognizerStateEnded || panGestureRecognize.state == UIGestureRecognizerStateCancelled) {
        //判断主控制的view的x是否超过屏幕宽度的一半
        if (self.mainVc.view.frame.origin.x > screenW * 0.2) {
            [self openLeftMenuVc];
        }else{
            [self coverBtnClick];
        }
    }else if (panGestureRecognize.state == UIGestureRecognizerStateChanged){
        self.mainVc.view.transform = CGAffineTransformMakeTranslation(offsetX, 0);
        self.leftMenuVc.view.transform = CGAffineTransformMakeTranslation(-self.leftWidth + offsetX, 0);
    }
}
//打开抽屉
-(void)openLeftMenuVc{
    [self.leftMenuVc viewDidLoad];
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.mainVc.view.transform = CGAffineTransformMakeTranslation(self.leftWidth, 0);
        
        self.leftMenuVc.view.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        //给mainvc的view添加遮盖按钮
        [self.mainVc.view addSubview:self.coverBtn];
    }];
}
//切换控制器
-(void)switchViewController:(UIViewController *)viewVC{
    
    viewVC.view.frame = self.mainVc.view.bounds;
    viewVC.view.transform = self.mainVc.view.transform;
    [self.view addSubview:viewVC.view];
    [self addChildViewController:viewVC];
    self.showVC = viewVC;
    [self coverBtnClick];
    [UIView animateWithDuration:0.25 animations:^{
        viewVC.view.transform = CGAffineTransformIdentity;
    }];
}
//回到主界面
-(void)backMainVc{
    
    [UIView animateWithDuration:0.25 animations:^{
        self.showVC.view.transform = CGAffineTransformMakeTranslation(self.view.frame.size.width, 0);
        [self updateLeftViewFrame];
    }completion:^(BOOL finished) {
        [self.showVC removeFromParentViewController];
        [self.showVC.view removeFromSuperview];
        self.showVC = nil;
    }];
    
}
- (void)updateLeftViewFrame{
    self.leftMenuVc.view.center = CGPointMake((CGRectGetMinX(self.mainVc.view.frame) + 280)/2, CGRectGetMidY(self.mainVc.view.frame));
}
-(void)coverBtnClick{
    
    [UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.mainVc.view.transform = CGAffineTransformIdentity;
        self.leftMenuVc.view.transform = CGAffineTransformMakeTranslation(-self.leftWidth, 0);
    } completion:^(BOOL finished) {
        [self.coverBtn removeFromSuperview];
        self.coverBtn = nil;
    }];
}
-(void)panRecognizer:(UIPanGestureRecognizer*)pan{
    
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    CGFloat offsetX = [pan translationInView:pan.view].x;
    if (offsetX > 0 ) return;
    CGFloat distance = self.leftWidth -ABS(offsetX);
    if (pan.state == UIGestureRecognizerStateEnded || pan.state == UIGestureRecognizerStateCancelled) {
        //判断主控制的view的x是否超过屏幕宽度的一半
        if (self.mainVc.view.frame.origin.x > screenW * 0.5) {
            
            [self openLeftMenuVc];
        }else{
            [self coverBtnClick];
        }
    }else if (pan.state == UIGestureRecognizerStateChanged){
        self.mainVc.view.transform = CGAffineTransformMakeTranslation(MAX(0, distance), 0);
        self.leftMenuVc.view.transform = CGAffineTransformMakeTranslation(-self.leftWidth + distance, 0);
    }
}
-(UIButton *)coverBtn{
    
    if (_coverBtn == nil) {
        _coverBtn = [[UIButton alloc]init];
        [_coverBtn addTarget:self action:@selector(coverBtnClick) forControlEvents:UIControlEventTouchUpInside];
        _coverBtn.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
        _coverBtn.frame = self.mainVc.view.bounds;
        //给按钮添加拖拽手势
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panRecognizer:)];
        [_coverBtn addGestureRecognizer:pan];
    }
    return _coverBtn;
}

@end
